<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class FinishProduct extends BaseModel
{
    use HasFactory;
    protected $table = 'finish_product';
    // 允许批量操作的字段
    protected $fillable = [
        'num',
        'sent_num',
        'to_be_sent_num',
        'semi_id',
        'warehouse_id',
        'goods_shelf_id',
        'model_number_id',
        'specification_id',
        'color_id',
        'customer_id',
        'order_number',
        'direction',
        'order_id',
        'is_del',
        'logistics_status',
        'logistics_info',
        'delivery_time',
        'warehouse_entry_time',
        'is_stock_up',
        'image1',
        'image2',
        'image3',
        'image4',
        'is_deduct_money',
        'is_check',
        'check_time',
        'created_at',
        'updated_at',
        'return_status'
    ];

    public function scopeSearch($query,$params)
    {
        //客户
        if(!empty($params['customer_id'])){
            $order_id = Order::where('customer_id',$params['customer_id'])->pluck('id')->toArray();
            $query->whereIn('order_id',$order_id);
        }
        //订单号
        if(!empty($params['order_number'])){
            $order_id = Order::where('order_number',$params['order_number'])->value('id');
            $query->where('order_id',$order_id);
        }
        //发货状态
        if(!empty($params['logistics_status'])){
            $query->where('logistics_status',$params['logistics_status']);
        }
        //产品
        if(!empty($params['product_id'])){
            $order_ids = Order::where('product_id',$params['product_id'])->pluck('id')->toArray();
            $query->whereIn('order_id',$order_ids);
        }
        //型号
        if(!empty($params['model_number_id'])){
            $order_ids = Order::where('model_number_id',$params['model_number_id'])->pluck('id')->toArray();
            $query->whereIn('order_id',$order_ids);
        }
        //流水号
        if(!empty($params['serial_number'])){
            $order_id = Order::where('serial_number',$params['serial_number'])->value('id');
            $query->where('order_id',$order_id);
        }
        //盘点状态
        if(!empty($params['is_check'])){
            $query->where('is_check',$params['is_check']);
        }
        if(!empty($params['start_time']) && !empty($params['end_time'])){
            $order_type = $params['order_type']??'finish_product';
            if($order_type == 'finish_product'){
                $query->whereBetween('warehouse_entry_time',[$params['start_time'],$params['end_time']]);
            }elseif ($order_type == 'pandian'){
                $query->where(function($q)use($params){
                    $q->whereBetween('check_time',[$params['start_time'],$params['end_time']])
                        ->orWhere(function($q){
                            $q->whereNull('check_time');
                        });
                });
            }
            elseif ($order_type == 'logistics_product'){
                $query->whereBetween('warehouse_entry_time',[$params['start_time'],$params['end_time']]);
            }

        }

        if(!empty($params['order_id'])){
            $query->where('order_id',$params['order_id']);
        }

        //退货换货
        if(!empty($params['return_status'])){
            if(is_array($params['return_status'])){
                $query->whereIn('return_status',$params['return_status']);
            }else{
                $query->where('return_status',$params['return_status']);
            }
        }


        $query->where('is_del',2);
        return $query;
    }

    public function order(){
        return $this->hasOne(Order::class,'id','order_id')
            ->with([
                'color',
                'product_name'=>function($q){
                return $q->select('id','product_name');
            },
                'customer'=>function($q){
                return $q->select('id','customer_name','customer_address','contact_phone','balance');
            },
                'model_number_name',
                'specification',
                'accountExecutive'=>function($q){
                    $q->select('id','company_name');
                },
                'warehouse',
                'order_second'=>function($q){
                    $q->with(['models','product','specification','model_number','color']);
                }
            ]);
    }

    public function stock_allocation_log(){
        return $this->hasMany(StockAllocationLog::class,'finish_product_id','id');
    }

    public function warehouse(){
        return $this->hasOne(Warehouse::class,'id','warehouse_id');
    }

    public function goods_shelf(){
        return $this->hasOne(GoodsShelf::class,'id','goods_shelf_id');
    }

    public function customer(){
        return $this->hasOne(Customer::class,'id','customer_id')
            ->select('id','customer_name','customer_address','contact_phone');
    }

    public function image1(){
        return $this->hasOne(File::class,'id','image1');
    }

    public function image2(){
        return $this->hasOne(File::class,'id','image2');
    }

    public function image3(){
        return $this->hasOne(File::class,'id','image3');
    }

    public function image4(){
        return $this->hasOne(File::class,'id','image4');
    }

    public function order_logistics(){
        return $this->hasMany(OrderLogistics::class,'finish_product_id','id');
    }

    public function semi_finish_product(){
        return $this->hasMany(SemiFinishProduct::class,'finish_id','id');
    }


}
